package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffTrialEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecOutStaffTrialEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffTrialEditBean bean = new UecOutStaffTrialEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 添加人员试用情况
	 */
	private UecTrial uecTrial;
	
	/**
	 * 流程定义key
	 */
//	private String procDefKey = "outStaffTrial";

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecOutStaffTrialEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		//是否结束endflag
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] endflag = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < endflag.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(endflag[i][j]);
				nodeVo.setName(endflag[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getEndflag(), nodeVoIshiskeypsnList);
		
		//试用类型trialType
		List<NodeVo> nodeVoTrialTypeList = new ArrayList<NodeVo>();
		String[][] trialType = { { "1", "转岗试用" }, { "2", "入职试用" } };
		for (int i = 0; i < trialType.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(trialType[i][j]);
				nodeVo.setName(trialType[i][j + 1]);
			}
			nodeVoTrialTypeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTrialType(), nodeVoTrialTypeList);

		//试用结果trialresult
		List<NodeVo> nodeVoResultList = new ArrayList<NodeVo>();
		String[][] trialresult = { { "0", "未通过试用" },{ "1", "转正" }, { "2", "延长试用期" } };
		for (int i = 0; i < trialresult.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(trialresult[i][j]);
				nodeVo.setName(trialresult[i][j + 1]);
			}
			nodeVoResultList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTrialresult(), nodeVoResultList);
		
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecOutStaffTrialEditWindow().setTitle("新增人员试用情况");
			bean.getUecOutStaffTrialEditDiv().setHeight("30%");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecOutStaffTrialEditWindow().setTitle("查看人员试用情况");
			uecTrial = (UecTrial) arg.get("uecTrial");
			if (null != uecTrial) {
				PubUtil.fillBeanFromPo(uecTrial, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecOutStaffTrialEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecOutStaffTrialEditWindow().setTitle("编辑人员试用情况");
			bean.getUecOutStaffTrialEditDiv().setHeight("30%");
			uecTrial = (UecTrial) arg.get("uecTrial");
			if (null != uecTrial) {
				PubUtil.fillBeanFromPo(uecTrial, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		if ("add".equals(opType)) {
			uecTrial = UecTrial.newInstance();
			PubUtil.fillPoFromBean(bean, uecTrial);
			dateTransToString();
			uecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecTrial.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecTrial.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
			//创建时间
			uecTrial.setCreationtime(DateUtil.getCurrenDate());
			//创建人
			uecTrial.setCreator(currentName);
			
			uecTrial.addOnly();
			Events.postEvent("onOK", this.self, uecTrial);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecTrial);
			dateTransToString();
			
			//修改人
			uecTrial.setModifier(currentName);
			//修改时间
			uecTrial.setModifiedtime(DateUtil.getCurrenDate());
			
			uecTrial.updateOnly();
			Events.postEvent("onOK", this.self, uecTrial);
			this.onCancel();
			Messagebox.show("数据更新成功！");
			
			//Messagebox.show("数据保存成功，外包员工转正流程已发起！");
			//uecTrial.updateOnly();
			//启动流程
//			String procDefId = "outStaffTrial:1:194bd82abd4147df8a20bd7a09f391bb";
//			String formTheme = "盟市人员转正审批流程";
//			String urgent = "1";
//			String formContent = "转正人:wangyiren";
//			String atiActCategoryId = "21";
//			String startFlowJson = "{" + 
//					   "formTheme: '盟市人员信息修改流程',"+
//					   " formContent: '信息修改人:wangyiren',"+
//					   " procDefId: 'outStaffEdit:1:11151202bbc143739de6f5eefdaace17',"+
//					   " urgent: '1',"+
//					   " atiActCategoryId: '21',"+
//					   " procDefKey: '" + ApprovalUtil.OUT_STAFF_TRIAL + "',"+
//					   " primaryId: " + uecTrial.getId() + 
//					"}"	;
//			ApprovalUtil.startApproval(startFlowJson);
						
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecOutStaffTrialEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getTrialType().getSelectedItem().getValue())) {
			return "请选择试用类型";
		} else if (StrUtil.isNullOrEmpty(bean.getTrialresult().getSelectedItem().getValue())) {
			return "请选择试用结果";
		} else if (StrUtil.isNullOrEmpty(bean.getEndflag().getSelectedItem().getValue())) {
			return "请选择试用是否结束";
		} else if (StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			return "请填写试用开始日期";
		} else if (StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			return "请填写试用结束日期";
		} else if (StrUtil.isNullOrEmpty(bean.getRegulardate().getValue())) {
			return "请填写转正日期";
		}
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 转正日期
		if (!StrUtil.isNullOrEmpty(bean.getRegulardate().getValue())) {
			uecTrial.setRegulardate(formatter.format(bean.getRegulardate()
					.getValue()));
		}
		// 开始日期
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecTrial.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		// 结束日期
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecTrial.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}

	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 转正日期
		if (null != uecTrial.getRegulardate()) {
			bean.getRegulardate()
					.setValue(DateUtil.patternStrToDate(uecTrial.getRegulardate()));
		}
		// 开始日期
		if (null != uecTrial.getBegindate()) {
			bean.getBegindate().setValue(DateUtil.patternStrToDate(uecTrial.getBegindate()));
		}
		// 结束日期
		if (null != uecTrial.getEnddate()) {
			bean.getEnddate().setValue(DateUtil.patternStrToDate(uecTrial.getEnddate()));
		}
	}
	
	

}
